#!/usr/bin/env bash
# ==============================================================================
# What:
# - simple script to use the Saxon Java library as a command line tool
#   for XSLT processing
#
# Usage:
#
#   $ saxon.sh <input.xml> <output.xml> <stylesheet.xsl>
#
#   <input.xml>      = path to the XML to transform
#   <output.xml>     = the output file to store the transformation result
#   <stylesheet.xsl> = the XSLT to apply to <input.xml>
# ==============================================================================
SELF=${0##*/}
BASE_DIR="$( cd "$( dirname "$0" )" && pwd )"

# Configurable parameters
SAXON_JAR=${BASE_DIR}/saxon-he-9.3.0.5.jar
CLASS_PATH=".:${SAXON_JAR}:${CLASS_PATH}"
INPUT=$1
OUTPUT=$2
XSLT=$3

# set maximum heap memory for Java
# - 512M = set maximum heap size to 512MB
# - increase value if the scripts fail with the following error:
#   Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
export JAVA_HEAP_MAX=${JAVA_HEAP_MAX:-512M}


if [[ $# != 3 ]]
then
   cat << EOF

  Usage:

    $ saxon.sh <input.xml> <output.xml> <stylesheet.xsl>

      <input.xml>      = path to the XML to transform
      <output.xml>     = the output file to store the transformation result
      <stylesheet.xsl> = the XSLT to apply to <input.xml>

EOF
   exit 1
fi


java -cp ${CLASS_PATH} -Xmx${JAVA_HEAP_MAX} net.sf.saxon.Transform -s:${INPUT} -o:${OUTPUT} -xsl:${XSLT}